﻿<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TransferPLV_INPUT2.aspx.vb" Inherits="WebForm_ManageSalesForecast_TransferPLV_INPUT2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TransferPLV_INPUT</title>
    <base target="_self"/>
    <style type="text/css">
    <!--
    body {
	    background-color: #170d0f;
	    margin-top: 0px;
	    margin-bottom: 0px;
    }
    -->
    </style>
    <style type="text/css">
    <!-- 
    .input_a{border: 1px solid #ccc;}
    .submit_a{border: 1px solid #ccc;}
    -->
    </style>
    <link id="Link2" href="~\css\set.css" runat="server" rel="stylesheet" type="text/css" />
    
<script type="text/vbscript">
    
    
Sub WorkSheet_SS_SheetChange(Sh, Target)
    amend_Color = vbMagenta
    set hf = document.getElementById("<%= XmlHiddenField.ClientID%>")
    if hf.value <> "" then
        exit sub
    end if

    set WorkSheet_SS = document.getElementById("WorkSheet_SS")
    WorkSheet_SS.EnableEvents = false

    For i = 1 to WorkSheet_SS.Selection.count
        if WorkSheet_SS.Selection.cells(i).Locked<>true then
            if not IsNumeric(WorkSheet_SS.Selection.cells(i).value) then
                msgbox WorkSheet_SS.Selection.cells(i).Address & " input value type error !! (Please input numeric type)"
                WorkSheet_SS.Selection.cells(i).value = ""
            else 
                WorkSheet_SS.Selection.cells(i).Interior.Color = amend_Color
            end if
        end if
    Next

    call ReservedAmendRows(WorkSheet_SS)
    WorkSheet_SS.EnableEvents = true
End Sub

Sub ReservedAmendRows(WorkSheet_SS)
    //分析欄位($A$1:$C$3--取得s_row=1,e_row=3)
    addr = Split(WorkSheet_SS.Selection.Address, ":")
    //目前所在的起始欄位
    For i = LBound(addr) To UBound(addr)
        row = Split(addr(i), "$")(2)
        If i = LBound(addr) Then
            s_row = row
        End If
        If i = UBound(addr) Then
            e_row = row
        End If
    Next

    For i = s_row  To e_row
        call AddSaveIndex(i)
    Next
  
End Sub


</script>



<script type="text/javascript">

        data = new Array();
        
        function saveData()
        {
            if (data.length == 0)
            {
                alert("no data changed");
                return;
            }
            
           
            var sheet = $get('WorkSheet_SS');
            
            //僅上傳有異動的資料行
            for (var i=0; i<data.length; i++)
            {
                var sf = data[i];
                
                sf.SapCode = sheet.cells(sf.RefIndex,1).value.replace('<%= MyObj.GetAreaCode() %>','');
                sf.Version1 = "<%= Version1 %>";
                sf.ChanelID = "<%= ChannelID %>";
                sf.SkuType =  sheet.cells(sf.RefIndex,<%= Last_Data_Col %> +1).value;
                //從報表所在年度的第一個月份開始
                var currFCSTCell = sheet.Range("<%= Start_Data_Cell %>");

                for (var mon = <%= FCSTMonth %>; mon <= <%= MonthLast %>;mon ++)
                {
                    sf['Mon'+mon] = sheet.cells(sf.RefIndex, currFCSTCell.column).value;
                    sheet.cells(sf.RefIndex, currFCSTCell.column).Interior.Color = '';
                    currFCSTCell = currFCSTCell.Cells(1,2); //下一個月
                }
            }
        
            //SFService.SaveData(data, OnComplete);
            SFService.SaveDataByV1(data,"<%= Version1 %>","<%= ChannelID %>" , '<%= Me.SessionID %>', OnComplete);
        }
        
        function AddSaveIndex(rowIndex)
        {
            //確認此 index 是否已被儲存
            for(var i=0; i<data.length; i++)
            {
                if (data[i].RefIndex == rowIndex) return;
            }
            
            var sf = new SFReport();        
        
            sf.RefIndex = rowIndex;
            data[data.length] = sf;           
        }
    
		function OnComplete(arg)
		{
			alert(arg);
		}
    
</script>    
    
    
    
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%">
    <tr>
        <td align="center" colspan="2" height="20px">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Label ID="lblTransferPLV_INPUT" SkinID="title" runat="server" meta:resourcekey="lblTransferPLV_INPUT"></asp:Label>
            <br/>
        </td>
    </tr>
    <tr>
    <td colspan="2">
       <object id="WorkSheet_SS" classid="clsid:0002E559-0000-0000-C000-000000000046" width="100%"
    height="425px" standby="Loading">
    <param name="DataType" value="XMLDATA"/>
    <param name="AllowPropertyToolbox" value="-1"/>
    <param name="AutoFit" value="0"/>
    <param name="Calculation" value="-4105"/>
    <param name="Caption" value="SF Forecast"/>
    <param name="DisplayColumnHeadings" value="-1"/>
    <param name="DisplayGridlines" value="-1"/>
    <param name="DisplayHorizontalScrollBar" value="-1"/>
    <param name="DisplayOfficeLogo" value="-1"/>
    <param name="DisplayPropertyToolbox" value="0"/>
    <param name="DisplayRowHeadings" value="-1"/>
    <param name="DisplayTitleBar" value="0"/>
    <param name="DisplayToolbar" value="-1"/>
    <param name="DisplayVerticalScrollBar" value="-1"/>
    <param name="DisplayWorkbookTabs" value="-1"/>
    <param name="EnableEvents" value="-1"/>
    <param name="MaxHeight" value="80%"/>
    <param name="MaxWidth" value="100%"/>
    <param name="MoveAfterReturn" value="-1"/>
    <param name="MoveAfterReturnDirection" value="-4121"/>
    <param name="RightToLeft" value="0"/>
    <param name="ScreenUpdating" value="-1"/>
    <param name="LockedDown" value="0"/>
    <param name="ConnectedToChart" value="0"/>
    <param name="DefaultQueryOnLoad" value="-1"/>
    <param name="EnableUndo" value="-1"/>
    </object>
<script type="text/vbscript">
    Sub WorkSheet_SS_SelectionChange()
        set WorkSheet_SS = document.getElementById("WorkSheet_SS")
        Dim Row 
	    Dim Col
	    MaxCol = WorkSheet_SS.ActiveSheet.UsedRange.Columns.Count 
        MaxRow = WorkSheet_SS.ActiveSheet.UsedRange.Rows.Count 
        sumvalue = 0 
        sumcount = 0 
        colType = "Number"
        for Each Row In WorkSheet_SS.Selection.Rows 
	        if Row.row > MaxRow then 
	            exit for 
            end if 
            for Each Col In WorkSheet_SS.Selection.Columns 
    	        if Col.column > MaxCol then 
 	 	            exit for 
                end if 
                if VarType(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value)>=2 and VarType(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value)<=5 then 
                    if sumcount=0 and InStr(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).NumberFormat,"%")<>0 then
                        colType = "Percent"
                    end if
                    if colType = "Percent" then 
                        sumvalue = sumvalue + (WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value * 100)
                    else
                        sumvalue = sumvalue + WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value 
                    end if
                    sumcount = sumcount + 1
                end if 
            next 
        next 

        if sumcount<>0 Then
            if colType = "Percent" then
                avg = Round(sumvalue / sumcount,2)
            else
                avg = Round(sumvalue / sumcount,0)
            end if
        else
            avg = 0
        end if
        if colType = "Percent" then
            window.status = "Average: " & FormatNumber(avg,2) & "%" & "  Count: " & FormatNumber(sumcount,0) & "  Sum: " & FormatNumber(sumvalue,2) & "%"
        else
            window.status = "Average: " & FormatNumber(avg,0) & "  Count: " & FormatNumber(sumcount,0) & "  Sum: " & FormatNumber(sumvalue,0)
        end if
    End Sub 
</script>
        <asp:HiddenField ID="XmlHiddenField" runat="server" />
    <script type="text/javascript">

        function set_SheetXmlData()
        {
            var sheet = document.getElementById("WorkSheet_SS")
            var hf = document.getElementById("<%= XmlHiddenField.ClientID%>");
            if (hf.value != "")
            {
                sheet.XMLData = hf.value;
                hf.value = "";
                sheet.ActiveSheet.Range("A2:C" + sheet.ActiveSheet.Rows.Count).AutoFilter();
                sheet.ActiveSheet.Columns.AutoFit();
                
                sheet.ActiveSheet.Range("D3").Select();//OWC11
                sheet.ActiveWindow.FreezePanes = true;

                sheet.ActiveSheet.Range("<%= Start_Data_Cell %>:<%= End_Data_Cell %>").Locked = false;
                sheet.ActiveSheet.Protect();  
                
            }
            if (sheet != undefined && '<%= Me.Locked %>'=='True')
            {
                sheet.activeSheet.cells.locked = true;
                sheet.activeSheet.protection.enabled = true;
            }
        }

        window.onload = function()
        {
            set_SheetXmlData();
        }
    </script>
    </td>
    </tr>
    <tr>
    <td align="center">
    <asp:Button ID="btnApply" runat="server" OnClientClick="Javascript:saveData();return false;"  meta:resourcekey="btnApply"  />
    <asp:Button ID="btnCancel" OnClientClick="Javascript:window.close()" runat="server" meta:resourcekey="btnCancel" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>
